Skip to content

Refactor (packages/ui/src/theme/color.ts): Extract RGB gamut check - #56

Open
magdang wants to merge 1 commit into
CMU-17313Q:mainfrom
magdang:refactor-rgb-gamut
Open

Refactor (packages/ui/src/theme/color.ts): Extract RGB gamut check#56
magdang wants to merge 1 commit into
CMU-17313Q:mainfrom
magdang:refactor-rgb-gamut

Conversation

@magdang

@magdang magdang commented Sep 3, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #52

Link to the associated GitHub issue:
#52

Full path to the refactored file:
packages/ui/src/theme/color.ts

What do you think this file does?
This file provides color utilities for the UI theme system, including color conversion and adjustment while ensuring generated colors remain within the valid RGB gamut.

What is the scope of your refactoring within that file?
I refactored the RGB gamut check inside the chroma-reduction loop in fitOklch and extracted it into isRgbInGamut.

Which Qlty-reported issue did you address?
I addressed the Complex binary expression reported in fitOklch. Before the refactor, Qlty reported two Complex binary expression issues in color.ts, and this refactor removed the targeted occurrence.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

This refactors the RGB gamut check in fitOklch's chroma-reduction loop into isRgbInGamut.

The helper uses the minimum and maximum RGB channel values to determine whether the output is within the valid 01 range. I added tests covering an in-gamut colour and an out-of-gamut colour whose chroma must be reduced.

How did the specific issue you chose impact the codebase’s maintainability?

The inline RGB gamut condition mixed validation logic with the chroma-reduction algorithm, making fitOklch harder to read and reason about.

What changes did you make to resolve the issue?

I extracted the RGB gamut condition into the isRgbInGamut helper, which uses the minimum and maximum RGB channel values to check whether all channels are within the valid 01 range.

How do your changes improve maintainability? Did you consider alternatives?

The helper gives the condition a clear name and separates RGB validation from the chroma-reduction logic, making fitOklch easier to understand and test. I considered leaving the condition inline, but extracting it reduces the complex expression without changing the existing behavior.

How did you verify your code works?

How did you validate that the change is correct?

I added tests for both an in-gamut colour and an out-of-gamut colour whose chroma must be reduced. I also ran the UI tests, coverage, CI, and Qlty before and after the refactor to confirm the behavior remained correct and the targeted smell was removed.

Focused coverage test:

cd packages/ui
bun test --coverage --coverage-reporter=lcov --coverage-dir=./coverage src/theme/color.test.ts

The coverage report includes src/theme/color.ts and executes the changed gamut-check path.

Assignment validation evidence

Q4.1 — Qlty smell reduction

Before and after screenshots of:

qlty smells --no-snippets packages/ui/src/theme/color.ts

The screenshots show the selected smell before the refactor and the reduced/removed result afterward.

Before: image

After: image

Q4.2 — Local lint and tests

Commands run:

bun lint
image

bun lint returns errors unrelated to my changes

cd packages/ui
bun test
image

Q4.3 — Coverage

The added packages/ui/src/theme/color.test.ts calls fitOklch for both in-gamut and out-of-gamut inputs. The latter executes the chroma-reduction loop and verifies the resulting RGB values are in gamut.

image

Q4.4 — CI

The CI unit-test workflow runs bun turbo test, which includes the UI package test command and discovers color.test.ts.

image

Screenshots / recordings

No visible UI behaviour changed. The validation screenshots are included above.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Extract the RGB gamut check used in the chroma-reduction loop and add tests for in-gamut and out-of-gamut fitOklch behavior.
@magdang
magdang marked this pull request as draft September 3, 2026 20:18
@magdang
magdang marked this pull request as ready for review September 3, 2026 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

P1B: Refactor (packages/ui/src/theme/color.ts:120): Complex binary expression

1 participant